﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBManager.UserControls {
    [ToolboxItem(true)]
    public partial class ucColumn : UserControl {
        public ucColumn() {
            InitializeComponent();
        }
        public event EventHandler Delete;

        public string ColumnName {
            get { return this.txtColumnName.Text; }
            set { this.txtColumnName.Text = value; }
        }

        private string oldColumnName;

        public string OldColumnName {
            get { return oldColumnName; }
            set { oldColumnName = value; }
        }
        
        public string ColumnType {
            get { return this.combColumnType.Text; }
            set { this.combColumnType.Text = value; }
        }

        public string FullColumnType {
            get {
                if (this.combColumnType.Text.ToLower().Contains("varchar")) {
                    return combColumnType.Text + "(" + this.txtPrecision.Text + ")";
                }
                else {
                    return combColumnType.Text;
                }
            }
        }

        public ColumnStaus Status;

        public bool ModifyEnable {
            set { 
                this.btnModify.Enabled = value;
                this.txtColumnName.ReadOnly = value;
                this.txtPrecision.ReadOnly = value;
                this.combColumnType.Enabled = !value;
            }
            get { return btnModify.Enabled; }
        }

        public int Percision {
            get {
                int temp = 0;
                int.TryParse(this.txtPrecision.Text, out temp);
                return temp;
            }
            set {
                if (ColumnType.ToLowerInvariant().Contains("varchar")) {
                    this.txtPrecision.Text = value.ToString();
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e) {
            
            Button btn = (Button)sender;
            bool isOriginalStatus = btn.Text == "修改";
            txtColumnName.ReadOnly = !isOriginalStatus;
            txtPrecision.ReadOnly = !isOriginalStatus;
            combColumnType.Enabled = isOriginalStatus;

            if (isOriginalStatus) {              
                this.Status = ColumnStaus.Update;
                btn.Text = "取消修改";
            }
            else {
                this.Status = ColumnStaus.UnChange;
                btn.Text = "修改";
            }
        }

        private void btnDel_Click(object sender, EventArgs e) {
            if (Delete != null) {
                Delete(sender, e);
            }
        }
    }
}
